﻿#define RunForWindwosEditor
#define RunForAndroidX


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Player : MonoBehaviour
{
    [SerializeField] private float rotateSpeed = 120.0f;
    private Transform followRotateTarget;
    private Rigidbody2D m_rigidbody2D;

    private Vector3 beforePos;
    private Coroutine getPosCoroutine;

    private AutoGenerateItems autoGenerateItems;

    [SerializeField] private GameObject diePre;


    private void Awake()
    {
        m_rigidbody2D = GetComponent<Rigidbody2D>();
        autoGenerateItems = GameObject.Find("/AutoGenerateItems").GetComponent<AutoGenerateItems>();
    }

    public void CollisionByCircle(Transform target)
    {
        rotateSpeed = 120.0f * (target.parent.localScale.x / 1.65f);
        followRotateTarget = target;
        m_rigidbody2D.bodyType = RigidbodyType2D.Static;
        getPosCoroutine = StartCoroutine("GetBeforePos");
    }


    private void CancelCollisionByCircle()
    {
        followRotateTarget = null;
        m_rigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        StopCoroutine(getPosCoroutine);
    }


    /// <summary>
    /// 增加抛物线力
    /// </summary>
    private void AddForece()
    {
        Vector2 dir = (transform.position - beforePos).normalized;

        if(dir.y > .65f)
        {
            m_rigidbody2D.AddForce(dir * rotateSpeed * 1.85f, ForceMode2D.Force);
            //Debug.Log($"posNormalized:{dir},beforePos:{beforePos}, currentPos:{transform.position}, force:{dir * rotateSpeed * 1.35f}");
        }
        else if(dir.y > .0f)
        {
            m_rigidbody2D.AddForce(dir * rotateSpeed * 1.65f, ForceMode2D.Force);
        }
        else
        {
            m_rigidbody2D.AddForce(dir * rotateSpeed * 0.95f, ForceMode2D.Force);
            //Debug.Log($"posNormalized:{dir},beforePos:{beforePos}, currentPos:{transform.position}, force:{dir * rotateSpeed * 0.65f}");
        }
    }

    /// <summary>
    /// 检测球体和玩家碰撞
    /// </summary>
    /// <returns></returns>
    public bool CheckCollisionByPlayerWithCircle() => followRotateTarget != null;


    /// <summary>
    /// 根据每两帧执行一次，来获取玩家之前的位置
    /// </summary>
    /// <returns></returns>
    private IEnumerator GetBeforePos()
    {
        while (CheckCollisionByPlayerWithCircle())
        {
            beforePos = transform.position;

            yield return new WaitForSeconds(1.0f / 50.0f * 8);
        }
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.name == "Bottom")
        {
            m_rigidbody2D.bodyType = RigidbodyType2D.Static;
            Instantiate(diePre, m_rigidbody2D.position, Quaternion.identity);
        }
    }



    private void FixedUpdate()
    {
        if(CheckCollisionByPlayerWithCircle())
        {
            transform.RotateAround(followRotateTarget.position, Vector3.forward, rotateSpeed * Time.deltaTime);
        }
    }




    private void Update()
    {

#if RunForWindwosEditor
        if (Input.GetMouseButtonDown(0))
        {
            if (CheckCollisionByPlayerWithCircle())
            {
                CancelCollisionByCircle();
                AddForece();
            }
        }
#elif RunForAndroidX
        if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            if (followRotateTarget != null)
            {
                CancelCollisionByCircle();
            }
        }
#endif
    }
}
